# coding:UTF-8

import scipy.io as scio
import numpy as np
from sklearn.model_selection import KFold  # 从sklearn导入KFold包

# 读取mat文件
def loadMatFlie():
    dataFile = './data/benchmarks.mat'
    data = scio.loadmat(dataFile)
    # print(data)
    datalist=list(data.keys())[3:-1]
    # ['banana', 'breast_cancer', 'diabetis', 'flare_solar', 'german', 'heart', 'image', 'ringnorm', 'splice', 'thyroid',
    #  'titanic', 'twonorm', 'waveform']
    with open('./data/description.csv', 'w', encoding='utf-8') as description:
        description.write(',数据量,数据维度,标签维度\n')
        for key in datalist:
            print('--------------------------------------------------------------------------------')
            print(key)
            arraylist=list(data[key][0][0])#去掉外两层
            description.write(key + ','+str(np.shape(arraylist[0])[0])+','+str(np.shape(arraylist[0])[1])+','+str(np.shape(arraylist[1])[1])+'\n')
            for temparray in arraylist:
                print(np.shape(temparray))
                # print(temparray)
                # print(type(temparray))
            # print(np.shape(data[key][0][0]))
            # print(type(data[key][0][0]))
    return data
# loadMatFlie()
# 加载单个数据集
def loadDataFromMat(dataname):
    dataFile = './data/benchmarks.mat'
    data = scio.loadmat(dataFile)
    # ['banana', 'breast_cancer', 'diabetis', 'flare_solar', 'german', 'heart', 'image', 'ringnorm', 'splice', 'thyroid',
    #  'titanic', 'twonorm', 'waveform']
    # print(dataname)
    arraylist = list(data[dataname][0][0])  # 去掉外两层
    return arraylist

#输入数据推荐使用numpy数组，使用list格式输入会报错
def K_Flod_spilt(K,fold,data,label):
    '''
    :param K: 要把数据集分成的份数。如十次十折取K=10
    :param fold: 要取第几折的数据。如要取第5折则 flod=5
    :param data: 需要分块的数据
    :param label: 对应的需要分块标签
    :return: 对应折的训练集、测试集和对应的标签
    '''
    split_list = []
    kf = KFold(n_splits=K)
    for train, test in kf.split(data):
        split_list.append(train.tolist())
        split_list.append(test.tolist())
    train,test=split_list[2 * fold],split_list[2 * fold + 1]
    return  data[train], data[test], label[train], label[test]  #已经分好块的数据集



